package com.mall507.fivepeaks.activity.mine;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.hzh.frame.comn.annotation.ViewInject;
import com.hzh.frame.comn.callback.HttpCallBack;
import com.hzh.frame.core.BaseSP;
import com.hzh.frame.core.HttpFrame.BaseHttp;
import com.hzh.frame.ui.activity.BaseUI;
import com.hzh.frame.widget.rxbus.RxBus;
import com.mall507.comn.model.User;
import com.mall507.comn.tools.UserTools;
import com.mall507.fivepeaks.R;


import org.json.JSONException;
import org.json.JSONObject;

@Route(path = "/mine/SettingEditnicknameUI")
public class SettingEditnicknameUI extends BaseUI{
    @ViewInject(R.id.nickname) //用户昵称
    EditText nickname;
    User mUser;
    
    @Override
    protected void onCreateBase() {
        mUser=UserTools.getInstance().getUser();
        setContentView(R.layout.ui_mine_setting_editnickname);
        getTitleView().setContent("修改昵称");
        getTitleView().setRightContent("完成");
        nickname.setText(mUser.getNickName());
        nickname.setSelection(mUser.getNickName().length());
        getTitleView().setRightOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editName();
            }
        });
    }
    
    private void editName(){
        if (TextUtils.isEmpty(nickname.getText().toString().trim())){
            alert("昵称不能为空,请重新输入");
        }else {
            BaseHttp.getInstance().query("Me/Edit", getParams(), new HttpCallBack() {
                @Override
                public void onSuccess(JSONObject response) {
                    if (response.optInt("code") == 200){
                        mUser.setNickName(nickname.getText().toString().trim());
                        UserTools.getInstance().updUser(mUser);
                        finish();
                    }
                    alert(response.optString("msg"));
                }
            });
        }
    }

    protected JSONObject getParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("type", 2);
            params.put("nickname", nickname.getText().toString().trim());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }
}
